package com.hwg.emp.web.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hwg.emp.common.util.DBUtil;
import com.hwg.emp.common.util.ThisAppUtil;
import com.hwg.emp.dto.AskoffDto;
import com.hwg.emp.model.UserMD;
import com.hwg.emp.service.AskoffService;
import com.hwg.emp.service.ServiceFactroy;
import com.hwg.emp.service.UserService;


public class AddEmpAskoffController extends HttpServlet{
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		req.setCharacterEncoding("utf8");
		AskoffService askoffService=ServiceFactroy.getService(AskoffService.class);
		UserService userService=ServiceFactroy.getService(UserService.class);
		String account=req.getParameter("addaccount");
		String startdate=req.getParameter("addstartdate");
		String enddate=req.getParameter("addenddate");
		String days=req.getParameter("adddays");
		String reason=req.getParameter("addreason");
		java.sql.Date currentStartDate=java.sql.Date.valueOf(startdate);
		java.sql.Date currentEndDate=java.sql.Date.valueOf(startdate);
		int currentDays=ThisAppUtil.parseInt(days, null);
		AskoffDto askoffDto=new AskoffDto();
		try {
			UserMD user = userService.queryUserByAccount(account);
			askoffDto.setId(DBUtil.uuid());
			askoffDto.setStartDate(currentStartDate);
			askoffDto.setEndDate(currentEndDate);
			askoffDto.setDays(currentDays);
			askoffDto.setReason(reason);
			askoffDto.setUser(user);
			boolean flag=askoffService.addEmpAskoff(askoffDto);
			if(flag){
				req.getRequestDispatcher("/selectEmpAskoffs.do").forward(req, resp);
			}
			else{
				req.setAttribute("erro", "对不起，添加失败");
				req.getRequestDispatcher("/selectEmpAskoffs.do").forward(req, resp);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
